package m.jclecai.com.base.recyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by andy on 2017/7/4.
 */

public abstract class BaseRecyclerViewAdapter<D> extends RecyclerView.Adapter<BaseViewHolder> {

    protected List<D> mDList;

    public BaseRecyclerViewAdapter(List<D> DList) {
        mDList = DList==null?new ArrayList<D>():DList;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =LayoutInflater.from(parent.getContext()).inflate(getItemView(),parent,false);
        return new BaseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        try {
            bindViewHolder(holder,mDList.get(position),position);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return mDList.size();
    }

    public void addDatas(List<D> DList){
        DList.remove(mDList);
        int DlistNumber = DList.size();
        int size = mDList.size();
        mDList.addAll(DList);
        //notifyItemRangeInserted(size,DlistNumber);
        notifyDataSetChanged();
    }

    public void clear(){
        mDList.clear();
        notifyDataSetChanged();
    }

    public List<D> getDataList() {
        return mDList;
    }

    public void refresh(List<D> DList) {

        clear();
        addDatas(DList);

    }

    public void addData(D d){
        int size = mDList.size();
        mDList.add(d);
        notifyItemInserted(size);
    }

    public abstract int getItemView();
    protected abstract void bindViewHolder(BaseViewHolder holder, D bean, int position) throws ParseException;
}
